﻿using System.Collections;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace CommonUtils
{
    public static class ControlUtil
    {
        public static void Bind(this ItemsControl itemsControl, IEnumerable data)
        {
            itemsControl.ItemsSource = data;
            if (itemsControl.GetType().Contains(typeof(Selector)) && (((Selector)itemsControl).SelectedIndex == -1))
                ((Selector)itemsControl).SelectedIndex = 0;
        }

        public static void Bind(this ItemsControl itemsControl, params object[] data)
        => itemsControl.Bind((IEnumerable)data);

        public static void AddItem<T>(this ItemsControl itemsControl, T item)
        {
            var list = itemsControl.ItemsSource as List<T>;
            if (list == null)
                list = new List<T>();
            list.Add(item);
            itemsControl.Bind(list);
        }

        public static void Disable(params Control[] controls)
        {
            controls.Each(control => control.Disable());
        }

        public static void Enable(params Control[] controls)
        {
            controls.Each(control => control.Enable());
        }
    }
}
